package com.panda.command;

import com.panda.common.FileHandler;
import com.panda.compile.Compiler;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class CompileOperation extends Operation {

    private Argument root = EmptyArgument.getInstance();

    {

        root.next(new SimpleArgument("f", "input")).next(new SimpleArgument("o", "output"));


    }


    @Override
    boolean canHandle(String ope) {
        return "cmp".equals(ope);
    }

    @Override
    void doHandle(String ope, List<String> args) throws Exception {

        Map<String, String> as = new HashMap<>();

        args.forEach(s -> root.handle(as, s));

        String input = as.get("input");

        if (input == null) {
            throw new RuntimeException("未指定文件输入,使用 -f 参数指定项目路径");
        }

        String output = as.get("output");

        if (output == null) {
            output = input + "/output";
        }

        Compiler compiler = new Compiler(new FileHandler(new File(input), new File(output), Console.charset));

        compiler.compile();

        Console.log("编译完成 文件输出至 '" + output + "'");

    }
}
